<template>
  <el-dialog v-model="dialogVisible" class="!border-rd-10px" title="新建年级" width="500">
    <el-form
      ref="ruleFormRef"
      style="max-width: 600px"
      :model="ruleForm"
      :rules="rules"
      label-width="auto"
      class="demo-ruleForm"
      :size="formSize"
      status-icon
    >
      <el-form-item label="新建类型">
        <el-radio-group v-model="ruleForm.type">
          <el-radio-button label="自定义" :value="0" />
          <el-radio-button label="小学" :value="1" />
          <el-radio-button label="初中" :value="2" />
          <el-radio-button label="高中" :value="3" />
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="ruleForm.type == 0" label="年级名称" prop="grade">
        <el-input v-model="ruleForm.grade" placeholder="请输入年级名称" />
      </el-form-item>
      <el-form-item v-else label="选择年级" prop="grade">
        <el-checkbox-group v-model="checkGrade" @change="handleCheckedCitiesChange">
          <el-checkbox v-for="grade in gradeList[ruleForm.type - 1]" :key="grade" :label="grade" :value="grade">
            {{ grade }}
          </el-checkbox>
        </el-checkbox-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="text-center">
        <el-button type="primary" @click="submitForm(ruleFormRef)">提交</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup lang="ts">
import { reactive, ref } from 'vue'
import type { ComponentSize, FormInstance, FormRules } from 'element-plus'

interface RuleForm {
  type: number
  grade: string
}

const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({
  type: 1,
  grade: '',
})
const checkGrade = ref([])
const gradeList = ref([
  ['一年级', '二年级', '三年级', '四年级', '五年级', '六年级'],
  ['初一', '初二', '初三'],
  ['高一', '高二', '高三']
])
const rules = reactive<FormRules<RuleForm>>({
  grade: [{ required: true, message: '请输入年级名称', trigger: 'blur' }],
  class: [
    {
      required: true,
      message: '请选择年级',
      trigger: 'change'
    }
  ]
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      // console.log('submit!')
    } else {
      // console.log('error submit!', fields)
    }
  })
}

const dialogVisible = ref(false)
function show(val) {
  dialogVisible.value = val
}
defineExpose({ show, dialogVisible })
</script>
<style scoped lang="scss"></style>
